#pragma once
#include<iostream>
#include<string>
#include <chrono>
#include <ctime>

namespace Ns_Log
{
    using namespace std;
    enum Level {
            INFO,
            DEBUG,
            WARNING,
            ERROR,
            FATAL
        };
    class Log // 提供一个接口，可以输出自定义日志信息，自动添加时间，所在文件，所在行，需要指定日志类型
    {
    public:
        
        static void _log(Level lv, const string& message, const char* filename, int line) {
            cout << "[";
            cout << lvToString(lv);
            cout << "]";

            cout << "[";
            cout << getCurrentTime();
            cout << "]";

            cout << "[";
            cout << filename;
            cout << "]";

            cout << "[";
            cout << "line:" << line;
            cout << "]";

            cout << "[";
            cout << message;
            cout << "]" << endl;
        }
        

    private:
        static string lvToString(Level lv) {
            switch (lv) {
                case INFO:   return "INFO";
                case DEBUG:  return "DEBUG";
                case WARNING:return "WARNING";
                case ERROR:  return "ERROR";
                case FATAL:  return "FATAL";
                default:     return "UNKNOWN";
            }
        }
        
        static string getCurrentTime() {
            auto now = std::chrono::system_clock::now();
            std::time_t now_c = std::chrono::system_clock::to_time_t(now);
            std::tm now_tm = *std::localtime(&now_c);
            
            return to_string(now_tm.tm_year + 1900) + "年" + to_string(now_tm.tm_mon + 1) + "月" + to_string(now_tm.tm_mday) + "日" + to_string(now_tm.tm_hour) + ":" + to_string(now_tm.tm_min) + ":" + to_string(now_tm.tm_sec);
        }
    };
}

// 使用宏简化日志记录的调用
#define log(lv, msg) Ns_Log::Log::_log(lv, msg, __FILE__, __LINE__)